//
//  TradesViewController.m
//  HAODE
//
//  Created by panquanReabam.com on 15/9/26.
//  Copyright (c) 2015年 reabam.com. All rights reserved.
//

#import "TradesViewController.h"
#import "OrderDetailViewController.h"
#import "UIImageView+WebCache.h"
#import "RDVTabBarController.h"
#import "MJRefresh.h"

#import "TradesCell.h"
#import "OrderDetailViewController.H"
#import "ServiceDetailViewController.H"
//#import "RefundDetailViewController.H"
#import "TheLoginViewController.h"
#import "PlaceOrderViewController.h"
#import "TradesModel.h"
#import "FilterView.h"
@interface TradesViewController ()<MBProgressHUDDelegate,UIGestureRecognizerDelegate,UINavigationControllerDelegate,UITextFieldDelegate,UISearchBarDelegate,FilterViewDelegate>
@end

@implementation TradesViewController

{

    NSMutableArray*modelArray;
    NSMutableArray*ScreeningModelArray;
    NSMutableArray*searchBeans;
    UITableView *table;
    UIButton*screeningBtn;
    int pageNumber;
    MBProgressHUD *progressHud;
    UIView*myToolBar;
    UIButton* screeningBtnImage;
    BOOL saveAccount;
    NSString* TotalCount;
    UIView*tableHeaderViews;
    UIButton*TotalCountStr;
}
@synthesize alt = alt;

-(void)BackAction:(UIButton *)sender{
    
    
     [self.navigationController popViewControllerAnimated:YES];
    
}
- (void)viewDidDisappear:(BOOL)animated

{
    
    [super viewDidDisappear:animated];
    
self.navigationController.delegate = nil;
    
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self setNavigation];
}


- (void)viewDidLoad
{
    
    [super viewDidLoad];
    
    modelArray=[[NSMutableArray alloc]init];
    table = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight) style:UITableViewStylePlain];
    table.delegate=self;
    table.dataSource=self;
    table.separatorStyle=UITableViewCellSeparatorStyleNone;
    [table registerClass:[TradesCell class] forCellReuseIdentifier:@"TradesViewController"];
    [self.view addSubview:table];
    [self tableHeaderViews];
    [self requestData];
   
    
}
-(void)requestData{
  pageNumber = 1;
    
    
    NSUserDefaults *user11 = [NSUserDefaults standardUserDefaults];
    NSString* Token11 = [user11 objectForKey:@"tokenId"];
    NSMutableDictionary *param11 = [NSMutableDictionary new];
    [param11 setValue:Token11 forKey:@"tokenId"];
    [param11 setValue:self.memberId forKey:@"memberId"];
    [param11 setValue:@"1" forKey:@"PageIndex"];
    [param11 setValue:@"50" forKey:@"PageSize"];
    NSLog(@"====%@",param11);
    
    [HttpRequest requestData:@"Business/Member/Trades" params:param11 success:^(BOOL isSuccess, SeverData *severDataDict) {
        if (isSuccess) {
  
    
            NSLog(@"====%@",severDataDict.dataDict);
            
            if ([[severDataDict.dataDict objectForKey:@"ResultInt"] integerValue]==0) {
                [Global hideProgressHUDInternal:self.view];
                if (searchBeans.count>0) {
                    
                    [screeningBtn setTitleColor:luseColor forState:UIControlStateNormal];
                    [screeningBtnImage setImage:[UIImage imageNamed:@"shaixuan_hight"] forState:UIControlStateNormal];
                    
                    
                }else{
                    [screeningBtn setTitleColor:shenhuiColor forState:UIControlStateNormal];
                    [screeningBtnImage setImage:[UIImage imageNamed:@"shaixuan"] forState:UIControlStateNormal];
                }
                
                NSMutableArray* arrr=[[NSMutableArray alloc]init];
                
                arrr=[severDataDict.dataDict objectForKey:@"DataLine"];
                
                if (arrr.count>0) {
                    NSString*GoldPrice=[NSString stringWithFormat:@"%@",[severDataDict.dataDict objectForKey:@"totalMoney"]];
                    NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
                    [numberFormatter setPositiveFormat:@"###,##0.00;"];
                    NSString *formattedNumberString = [numberFormatter stringFromNumber:[NSNumber numberWithDouble:GoldPrice.floatValue]];

                    TotalCount=[severDataDict.dataDict objectForKey:@"TotalCount"];
                    [TotalCountStr setTitle:[NSString stringWithFormat:@"   共 %@ 条 ¥%@",TotalCount,formattedNumberString] forState:UIControlStateNormal];

                    NSMutableArray * mItemsArray=[NSMutableArray arrayWithArray:[severDataDict.dataDict objectForKey:@"DataLine"]];
                    for (NSDictionary *dic in mItemsArray) {
                        TradesModel *dataModel=[TradesModel dataModelWithDict:dic];
                        [modelArray addObject:dataModel];
                        
                    }
                    [table reloadData];
                    
                }
                
                
            }else{
                [Global hideProgressHUDInternal:self.view];
                if ([[severDataDict.dataDict objectForKey:@"ResultInt"] integerValue]==9999) {
                    
                    
                    TheLoginViewController *detailViewController = [[TheLoginViewController alloc] init];
                    [self.navigationController pushViewController:detailViewController animated:YES];
                }
                
            }
            
        }
        else
        {
            [Global hideProgressHUDInternal:self.view];
            
            [Global showTextHudSub:@"连接失败" view:self.view];
        }
        
    }];
    
    
    
    
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    
    return [modelArray count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    TradesCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TradesViewController"];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    cell.model=modelArray[indexPath.row];
    
    return cell;
 
}



- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    
    return  108;
    
}




#pragma mark - Table view delegate


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    TradesModel *dataModel=modelArray[indexPath.row];
    
        
    
                if ([dataModel.orderType  isEqualToString:@"order"]) {
                    
                    
                    OrderDetailViewController *detailViewController = [[OrderDetailViewController alloc] init];
                    detailViewController.orderId=dataModel.orderId;
                    [self.navigationController pushViewController:detailViewController animated:YES];
                    
                }
                
                if ([dataModel.orderType isEqualToString:@"service"]) {
                 
                    ServiceDetailViewController *detailViewController = [[ServiceDetailViewController alloc] init];
                         detailViewController.orderId=dataModel.orderId;
                    [self.navigationController pushViewController:detailViewController animated:YES];
                    
                    
                    
                }
                if ([dataModel.orderType isEqualToString:@"refund"]) {
//                
//                    RefundDetailViewController *detailViewController = [[RefundDetailViewController alloc] init];
//                         detailViewController.orderId=dataModel.orderId;
//                    [self.navigationController pushViewController:detailViewController animated:YES];
                    
                }
    
    


}



#pragma mark  筛选后回调

- (void)showAlertWait
{
    alt = [[UIAlertView alloc] initWithTitle:@"网络不稳定 ！" message:@"请确认网络" delegate:self cancelButtonTitle:nil otherButtonTitles: nil];
    [alt show];
    
    [self performSelector:@selector(hideAlert) withObject:nil afterDelay:1.0f];
    
}

- (void)showAlertMessage:(NSString *)msg
{
    alt = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:msg delegate:nil cancelButtonTitle:nil otherButtonTitles: nil];
    [alt show];
    
    [self performSelector:@selector(hideAlert) withObject:nil afterDelay:1.0f];
}
- (void)hideAlert
{
    if (alt != nil)
    {
        [alt dismissWithClickedButtonIndex:0 animated:YES];
        alt = nil;
    }
}

-(void)setNavigation{
    
    
    [[self rdv_tabBarController] setTabBarHidden:YES];
    self.navigationController.navigationBarHidden=NO;
    self.navigationController.navigationBar.barStyle = UIBaselineAdjustmentNone;
    UILabel *lineLabel=[[UILabel alloc] initWithFrame:CGRectMake(0, 43,ScreenWidth, 1)];
    [lineLabel setBackgroundColor:[UIColor colorWithRed:238.0/255.0 green:238.0/255.0 blue:238.0/255.0 alpha:1.0]];
    [self.navigationController.navigationBar addSubview:lineLabel];
    [self.navigationController.navigationBar setTintColor:[UIColor whiteColor]];
    [[UINavigationBar appearance] setBarTintColor:Color];
    
    UIImage* backImage = [UIImage imageNamed:@"fanhui"];
    CGRect backframe = CGRectMake(0,0,30,30);
    UIButton* backButton= [[UIButton alloc] initWithFrame:backframe];
    [backButton setBackgroundImage:backImage forState:UIControlStateNormal];
    backButton.titleLabel.textAlignment=NSTextAlignmentLeft;
    [backButton addTarget:self action:@selector(BackAction:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem* leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    self.navigationItem.leftBarButtonItem = leftBarButtonItem;
    self.navigationController.delegate = self;
    self.navigationController.interactivePopGestureRecognizer.enabled = YES;
    self.navigationController.interactivePopGestureRecognizer.delegate = self;
    
    [self.navigationController.navigationBar setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
    self.navigationController.navigationBar.shadowImage = [[UIImage alloc] init];
    
    self.title=@"交易明细";
       

    
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    NSString* Token = [user objectForKey:@"tokenId"];
    if (Token ==nil) {
        Token=@"dd";
    }
    NSMutableDictionary *param = [NSMutableDictionary new];
    [param setValue:Token forKey:@"tokenId"];
    [param setValue:@"order" forKey:@"filterType"];
//    NSLog(@"====%@",param);
    [Global showIndeterminateHudSub:@"加载中，请稍后。。。" delay:30 view:self.view];
    [HttpRequest requestData:@"Common/AdvancedFilterList" params:param success:^(BOOL isSuccess, SeverData *severDataDict) {
        if (isSuccess) {
            
//            NSLog(@"====－－－－－－－%@",severDataDict.dataDict);
            
            
            if ([[severDataDict.dataDict objectForKey:@"ResultInt"] integerValue]==0) {
                [Global hideProgressHUDInternal:self.view];
                
                NSMutableArray * mItemsArray=[NSMutableArray arrayWithArray:[severDataDict.dataDict objectForKey:@"DataLine"]];
                for (NSDictionary *dic in mItemsArray) {
                    MemberListModel *dataModel=[MemberListModel ScreeningDataModelWithDict:dic];
                    [ScreeningModelArray addObject:dataModel];
                    
                }
                
                
                
            }else{
                [Global hideProgressHUDInternal:self.view];
                NSString *requestResultStr=[severDataDict.dataDict objectForKey:@"ResultString"];
                [Global showTextHudSub:requestResultStr view:self.view];
            }
            
        }
        else
        {
            [Global hideProgressHUDInternal:self.view];
            
            [Global showTextHudSub:@"连接失败" view:self.view];
        }
        
    }];
    

    
    
}

-(void)tableHeaderViews{
    
    
    tableHeaderViews=[[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth,80/2)];
    tableHeaderViews.backgroundColor= zuiqianhuiColor;
    table.tableHeaderView=tableHeaderViews;
    TotalCountStr=[[UIButton alloc]initWithFrame:CGRectMake(5, 0, ScreenWidth*430/750, 80/2)];
    [TotalCountStr setTitleColor:shenhuiColor forState:UIControlStateNormal];
    TotalCountStr.titleLabel.font=[UIFont systemFontOfSize:14];
    TotalCountStr.contentHorizontalAlignment=UIControlContentHorizontalAlignmentLeft;
    [tableHeaderViews addSubview:TotalCountStr];
    screeningBtn=[[UIButton alloc]initWithFrame:CGRectMake(ScreenWidth*590/750,  0, 70,40)];
    [screeningBtn.layer setCornerRadius:2.0];
    [screeningBtn.layer setBorderWidth:0];
    [screeningBtn.layer setMasksToBounds:YES];
      [screeningBtn addTarget:self action:@selector(sxuan:) forControlEvents:UIControlEventTouchUpInside];
    CGColorSpaceRef colorSpace2 = CGColorSpaceCreateDeviceRGB();
    CGColorRef colorref2 = CGColorCreate(colorSpace2,(CGFloat[]){153/255.0,153/255.0, 153/255.0, 1.0   });
    [screeningBtn.layer setBorderColor:colorref2];//边框颜色
    [screeningBtn setTitle:@"     筛选" forState:UIControlStateNormal];
    screeningBtn.titleLabel.font=[UIFont systemFontOfSize:15];
    [screeningBtn setTitleColor:shenhuiColor forState:UIControlStateNormal];
    screeningBtn.titleLabel.textAlignment=NSTextAlignmentRight;
    [tableHeaderViews addSubview:screeningBtn];
    
    screeningBtnImage = [[UIButton alloc] initWithFrame:CGRectMake(3,5, 30, 30)];
    [screeningBtnImage setImage:[UIImage imageNamed:@"shaixuan"] forState:UIControlStateNormal];
      [screeningBtnImage addTarget:self action:@selector(sxuan:) forControlEvents:UIControlEventTouchUpInside];
    [screeningBtn addSubview:screeningBtnImage];
}

-(void)sxuan:(UIButton*)sender{
    FilterView *filterView=[FilterView FilterViewWithSize:CGSizeMake(ScreenWidth, ScreenHeight)];
    filterView.typeStr=@"whsIn";
    filterView.delegate=self;
    filterView.selectedSearchBeansArray=searchBeans;
    [filterView show];
    
}
#pragma mark FilterViewDelegate
-(void)confirmFilter:(NSMutableArray *)searchBeansArray
{
    searchBeans=searchBeansArray;
    [self requestData];
}


@end

